/*
 * @Author: NtyAaron
 * @Date: 2021-03-23 14:46:41
 * @LastEditors: NtyAaron
 * @LastEditTime: 2021-06-04 18:24:39
 * @Description: file content
 * @FilePath: \dwq\pages\pubService\pubService.js
 */

import { saveCollect, searchHospitalPage, searchPolicePage } from '../../api/pubService/pubService.js'
import { _t } from '../../i18n/base'

var lang
Page({
  data: {
    active: 0,
    test: '',
    hospitalList: [],
    policeList: [],
    keyword: '',
    hospitalPage: 1,
    policPage: 1,
    searchInfo: {
      keyword: '',
      lat: '',
      lng: '',
      page: 1,
    },
    userId: wx.getStorageSync('userId'),
    isNoHospitalList: false,
    isNoPoliceList: false,
  },
  // tab 点击
  tabChangeHandle(e) {
    this.setData({
      active: e.detail.index,
    })
    // if (this.data.language != 3) {
    //   this.searchHandle()
    // }
  },
  // 服务收藏
  async serviceCollectHandle(e) {
    let res = e.currentTarget.dataset.info
    let info = {
      address: res.address,
      locationName: res.name,
      userId: this.data.userId,
    }
    let result = await saveCollect(info)
    if (result.code == 200) {
      wx.showToast({
        title: '收藏成功',
      })
    }
  },
  // 底部加载
  async scrollEnd(e) {
    let type = e.currentTarget.dataset.type
    if (type == 1) {
      this.setData({
        hospitalPage: this.data.hospitalPage + 1,
      })
      this.setData({
        ['searchInfo.page']: this.data.hospitalPage,
      })
      let resHospital = await searchHospitalPage(this.data.searchInfo)
      if (resHospital.result.pois.length) {
        this.setData({
          hospitalList: this.data.hospitalList.concat(resHospital.result.pois),
        })
      }
    } else {
      this.setData({
        policPage: this.data.policPage + 1,
      })
      this.setData({
        ['searchInfo.page']: this.data.policPage,
      })
      let { result } = await searchPolicePage(this.data.searchInfo)
      if (result.pois.length) {
        this.setData({
          policeList: this.data.policeList.concat(result.pois),
          isNoPoliceList: false,
        })
      }
    }
  },

  // 拨打电话
  makeCallHandle(e) {
    if (e.currentTarget.dataset.info == '') {
      wx.showToast({
        title: this.data.lang['暂无联系方式'],
      })
    } else {
      wx.showModal({
        cancelColor: '#F95F22',
        cancelText: this.data.lang['取消'],
        confirmColor: '#999999',
        confirmText: this.data.lang['确定'],
        content: this.data.lang['前往拨打界面'],
        showCancel: true,
        success: (result) => {
          if (result.confirm) {
            wx.makePhoneCall({
              phoneNumber: e.currentTarget.dataset.info,
            })
          }
        },
      })
    }
  },
  // 跳转地图
  addressToMapHandle(e) {
    let info = JSON.stringify(e.currentTarget.dataset.info)
    wx.navigateTo({
      url: `/pages/interactionMap/interactionMap?location=${info}&locaInfo=${JSON.stringify(this.data.locaInfo)}`,
    })
  },
  //查看医院详情
  ToDetailHandle(e) {
    let info = JSON.stringify(e.currentTarget.dataset.info)
    wx.navigateTo({
      url: `/pages/hospitalDetail/hospitalDetail?detail=${info}`,
    })
  },
  // 跳转详情页
  toDetail(e) {
    let type = e.currentTarget.dataset.type
    wx.navigateTo({
      url: '/pages/pubDetail/pubDetail?type=' + type,
    })
  },
  // 获取值
  inputHandle({ detail }) {
    this.setData({
      keyword: detail.value,
    })
  },
  //搜索
  async searchHandle() {
    let res = await wx.getLocation({
      type: 'wgs84',
      altitude: false,
    })
    this.setData({
      ['searchInfo.lat']: res.latitude,
      ['searchInfo.lng']: res.longitude,
      ['searchInfo.keyword']: this.data.keyword,
    })
    if (this.data.active == 0) {
      this.setData({
        ['searchInfo.page']: this.data.hospitalPage,
      })
      this.getHospital()
    } else {
      this.setData({
        ['searchInfo.page']: this.data.policPage,
      })
      this.getPolice()
    }
  },
  // 获取当前定位
  async getCurrentLocation() {
    this.setData({
      ['searchInfo.lat']: this.data.locaInfo.latitude,
      ['searchInfo.lng']: this.data.locaInfo.longitude,
    })
    this.getHospital()
    this.getPolice()
  },
  // 获取医院数据
  async getHospital() {
    let resHospital = await searchHospitalPage(this.data.searchInfo)
    if (resHospital.result.pois.length == 0) {
      this.setData({
        hospitalList: [],
        isNoHospitalList: true,
      })
    } else {
      this.setData({
        hospitalList: resHospital.result.pois,
        isNoHospitalList: false,
      })
    }
  },
  // 获取警察局数据
  async getPolice() {
    let { result } = await searchPolicePage(this.data.searchInfo)
    if (result.pois.length == 0) {
      this.setData({
        policeList: [],
        isNoPoliceList: true,
      })
    } else {
      this.setData({
        policeList: result.pois,
        isNoPoliceList: false,
      })
    }
  },
  // 复制
  copyLink({ currentTarget }) {
    wx.setClipboardData({
      data: currentTarget.dataset.link,
      success: () => {
        wx.showToast({
          title: this.data.lang['已复制网站网址'],
          icon: 'none',
        })
      },
    })
  },
  onLoad(e) {
    lang = _t()
    this.setData({
      lang,
      language: wx.getStorageSync('lang'),
      isOverseas: JSON.parse(e.isOverseas),
      ['searchInfo.language']: wx.getStorageSync('lang'),
    })
    wx.setNavigationBarTitle({
      title: lang['公共服务'],
    })
    if (e.locaInfo) {
      this.setData({
        locaInfo: JSON.parse(e.locaInfo),
      })
    }
    this.getCurrentLocation()
  },
  // 每次进入页面
  onShow() {
    // if (this.data.language != 3) {
    //   this.getCurrentLocation()
    // }
  },
})
